using System;
using System.Web.Mvc;
using TfsReports.Web.Extensions;
using TfsReports.Web.Helpers;
using TfsReports.Web.Models;

namespace TfsReports.Web.Controllers
{
    public class BuildController : Controller
    {
        public BuildController()
        {
            TempDataProvider = new NoTempDataProvider();
        }

        /// <summary>
        /// Generates an Index view of <see cref="Build"/>
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="start">The start.</param>
        /// <param name="stop">The stop.</param>
        /// <returns></returns>
        /// <example>GET: /Build/</example>
        public ActionResult Index(int? page, int? pageSize, DateTime? start, DateTime? stop)
        {
            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var builds = TfsWarehouse.GetAllBuilds();
            builds.Sort((b1, b2) => b2.Start.CompareTo(b1.Start));
            if (!stop.HasValue)
                stop = builds[0].Start;
            if (!start.HasValue)
                start = builds[builds.Count - 1].Start;
            var filteredBuilds = builds.FindAll(b => b.Start > start && b.Start < stop);
            filteredBuilds.Sort((b1, b2) => b1.Id.CompareTo(b2.Id));
            ViewData["Title"] = string.Format("Builds from {0} to {1}", ((DateTime)start).ToString("yyyy-MM-dd"), ((DateTime)stop).ToString("yyyy-MM-dd"));
            return View(filteredBuilds.ToPagedList(currentPageIndex, pageSize ?? 10));
        }

        /// <summary>
        /// Generates an Details view of <see cref="Build"/>
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        /// <example>GET: /Build/Details/5</example>
        public ActionResult Details(int id)
        {
            return View(TfsWarehouse.GetAllBuilds().Find(b => b.Id == id));
        }
    }
}
